package gizwits.centerair.function;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class test {
	
	public static void main(String[] args) {
		//log(根据homeId获取房间列表.run(494103));
		List<Deviv> lis = new ArrayList<Deviv>();
		
		lis.add(new Deviv("aa","11"));
		lis.add(new Deviv("bb","12"));
		lis.add(new Deviv("cc","13"));
		lis.add(new Deviv("dd","14"));
		lis.add(new Deviv("ee","15"));
		
		for(Deviv d : lis){
			d.setAge("88");
			d.setName("QQ");
		}
		
		for (int i = 0; i < lis.size(); i++) {
			Deviv dd = lis.get(i);
			System.out.println("["+dd.getName()+","+dd.getAge()+"]");
		}
		
		String json = "[{\"dids\":\"194989\",\"roomId\":\"978879-2-9A-5\"},{\"dids\":\"194962\",\"roomId\":\"978879-2-9A-3\"},{\"dids\":\"194953\",\"roomId\":\"978879-2-9A-2\"},{\"dids\":\"195002\",\"roomId\":\"978879-2-9A-4\"},{\"dids\":\"194957\",\"roomId\":\"978879-2-9A-1\"},{\"dids\":\"194964\",\"roomId\":\"978879-2-9A-8\"},{\"dids\":\"194977\",\"roomId\":\"978879-2-9A-6\"},{\"dids\":\"194961\",\"roomId\":\"978879-2-9A-7\"},{\"dids\":\"194985\",\"roomId\":\"978879-2-9A-9\"}]";
		String jsons = "[]";
		JSONArray array = JSONObject.parseArray(jsons);
		String sql = "UPDATE Device SET roomId='%s' WHERE did=%s";
		for(int i=0; i<array.size(); i++){
			JSONObject o = array.getJSONObject(i);
//			StringBuffer sql = new StringBuffer().append("UPDATE Device SET roomId=");
//			sql.append("'").append(o.get("roomId")).append("'");
//			sql.append(" WHERE did=").append(o.get("dids"));
			System.out.println(String.format(sql, o.get("roomId"), o.get("dids")));
		}
		
		System.out.println(getDids(array));
	}
	
	public final static void log(Object obj){
		System.out.println(obj);
	}
	
	private static String getDids(JSONArray arrays){
		StringBuffer sb = new StringBuffer();
		if(arrays != null && arrays.size() > 0){
			int len = arrays.size();
			for(int i=0;i<len;i++){
				JSONObject didJson = arrays.getJSONObject(i);
				sb.append(",").append(didJson.get("dids"));
			}
			return sb.substring(1);
		}
		return "";
	}
}
